/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import Excepciones.ExcepcionExisteNombreDiccionario;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
/**
 *
 * @author Jose Luis Orta
 */
public class VistaPrincipal extends JFrame {
    private JMenuBar menuPrincipal;
    private SpringLayout layout;
    private GridBagLayout layout2;
    private JToolBar toolbar;
    private JPanel panelPrincipal;
    private CIPrincipal ci;
    private JDialog creardic;
    private JPanel pcrear;
    private SpringLayout layoutDialogoCrear;
    private JButton btnVisDicc;
    private JButton btnVisReg;
    private JButton btnVisCP;
    private JButton btnVisCor;


    public VistaPrincipal (CIPrincipal c,String titulo){
        this.setTitle(titulo);
        ci=c;
        inicializar();
        
    }


    public VistaPrincipal(CIPrincipal c){
        this.setTitle("Default title");
        ci=c;
        inicializar();
    }

    public void visualizar(){
        this.setVisible(true);
    }

    public void cambiarAVistaDiccionario(){
        cambiarVistas(false,true,false,false,false);
    }
    public void cambiarAVistaConjPares(){
        cambiarVistas(false,false,true,false,false);
        //ci.inicializarVCP();
    }
    public void cambiarAVistaCorregir(){
        cambiarVistas(false,false,false,true,false);
    }
    public void cambiarAVistaReglas(){
        cambiarVistas(true,false,false,false,false);
    }
  
    private void inicializar(){
        //contructoras
        toolbar = new JToolBar();
        panelPrincipal = new JPanel();
        layout = new SpringLayout();
        layout2 = new GridBagLayout();
        menuPrincipal = new JMenuBar();
        btnVisDicc = new JButton();
        btnVisReg = new JButton();
        btnVisCP = new JButton();
        btnVisCor = new JButton();


        //el frame
        inicializarVentana();


        //inicializaciones varias
        inicializarMenu();
        inicializarToolbar();
        inicializarPanelPrincipal();
        inicializarLayout();
        inicializarLayout2();

        //montar los componentes
        montar();
    }
    private void inicializarLayout(){
        //seleccionamos el layout para este componente
        this.setLayout(layout);
        layout.putConstraint(SpringLayout.NORTH, menuPrincipal, 0, SpringLayout.NORTH, this.getContentPane());
        layout.putConstraint(SpringLayout.WEST, menuPrincipal, 0, SpringLayout.WEST, this.getContentPane());
        layout.putConstraint(SpringLayout.EAST, menuPrincipal, 0, SpringLayout.EAST, this.getContentPane());
        layout.putConstraint(SpringLayout.NORTH, toolbar, 0, SpringLayout.SOUTH, menuPrincipal);
        layout.putConstraint(SpringLayout.WEST, toolbar, 0, SpringLayout.WEST, this.getContentPane());
        layout.putConstraint(SpringLayout.EAST, toolbar, 0, SpringLayout.EAST, this.getContentPane());
        layout.putConstraint(SpringLayout.EAST, panelPrincipal, 0, SpringLayout.EAST, this.getContentPane());
        layout.putConstraint(SpringLayout.WEST, panelPrincipal, 0, SpringLayout.WEST, this.getContentPane());
        layout.putConstraint(SpringLayout.NORTH, panelPrincipal, 0, SpringLayout.SOUTH, toolbar);
        layout.putConstraint(SpringLayout.SOUTH, panelPrincipal, 0, SpringLayout.SOUTH, this.getContentPane());

    }
 
    private void inicializarVentana(){
        this.setLocationByPlatform(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setMinimumSize(new Dimension(1030,500));
        this.setLocationByPlatform(true);
        this.setSize(800,600);
        this.setLocationRelativeTo(null);
    }
    private void inicializarMenu(){

        JMenu m1 = new JMenu("Archivo");
        JMenu m2 = new JMenu("Edición");
        JMenu m3 = new JMenu("Diccionario");
        JMenu m5 = new JMenu("Corregir");
        JMenu m6 = new JMenu("Conjunto de Pares");
        JMenu m7 = new JMenu("Reglas");
        JMenu m4 = new JMenu("Ayuda");
        JMenuItem m4i1 = new JMenuItem("Mostrar Ayuda");
        JMenuItem m1i1 = new JMenuItem("Crear texto");
        JMenuItem m1i2 = new JMenuItem("Cargar texto...");
        JMenuItem m1i3 = new JMenuItem("Salvar texto...");
        JMenuItem m1i4 = new JMenuItem("Salir");
        JMenuItem m1i5 = new JMenuItem("Cerrar texto");
        JMenuItem m2i1 = new JMenuItem("Seleccionar todo");
        JMenuItem m2i2 = new JMenuItem("Copiar");
        JMenuItem m2i3 = new JMenuItem("Cortar");
        JMenuItem m2i4 = new JMenuItem("Pegar");
        JMenuItem m3i1 = new JMenuItem("Crear diccionario");
        JMenuItem m3i2 = new JMenuItem("Cargar diccionario");
        JMenuItem m3i3 = new JMenuItem("Salvar diccionario");
        JMenuItem m3i4 = new JMenuItem("Eliminar diccionario");
        JMenuItem m5i1 = new JMenuItem("Empezar correción");
        JMenuItem m5i2 = new JMenuItem("Finalizar correción");
        JMenuItem m6i1 = new JMenuItem("Crear conjunto de pares");
        JMenuItem m6i2 = new JMenuItem("Cargar conjunto de pares");
        JMenuItem m6i3 = new JMenuItem("Salvar conjunto de pares");
        JMenuItem m6i4 = new JMenuItem("Eliminar conjunto de pares");
        JMenuItem m7i1 = new JMenuItem("Generar reglas");
        
        m1i1.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m1i1ActionPerformed(e);
            }
        });

        m1i4.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m1i4ActionPerformed(e);
            }
        });

        m2i1.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m2i1ActionPerformed(e);
            }
        });

        m2i2.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m2i2ActionPerformed(e);
            }
        });

        m2i3.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m2i3ActionPerformed(e);
            }
        });

        m2i4.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m2i4ActionPerformed(e);
            }
        });

        m1i2.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m1i2ActionPerformed(e);
            }

        });

       m1i3.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m1i3ActionPerformed(e);
            }

        });

       m1i5.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m1i5ActionPerformed(e);
            }

        });

        m3i1.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m3i1ActionPerformed(e);
            }
        });

        m3i2.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                try {
                    try {
                        m3i2ActionPerformed(e);
                    } catch (ExcepcionExisteNombreDiccionario ex) {
                        Logger.getLogger(VistaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (ExcepcionVacio ex) {
                    Logger.getLogger(VistaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

        m3i3.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m3i3ActionPerformed(e);
            }
        });

        m3i4.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m3i4ActionPerformed(e);
            }
        });
        
        m5i1.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m5i1ActionPerformed(e);
                ci.corregir();
            }
        });
        
        m5i2.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m5i2ActionPerformed(e);
                ci.finalizarcorreccion();
            }
        });
        
        m6i1.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m6i1ActionPerformed(e);
            }
        });
        
        m6i2.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                try {
                    m6i2ActionPerformed(e);
                } catch (FormatoConjParesNoValido ex) {
                    Logger.getLogger(VistaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(VistaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        
        m6i3.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m6i3ActionPerformed(e);
            }
        });

        m6i4.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                m6i4ActionPerformed(e);
            }
        });
        
        m7i1.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                try {
                    m7i1ActionPerformed(e);
                } catch (ExcepcionVacio ex) {

                }


            }
        });

        m4i1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                m4i1ActionPerformed(e);
            }
        });
        
        m1.add(m1i1);
        m1.add(m1i2);
        m1.add(m1i3);
        m1.add(m1i5);
        m1.add(m1i4);
        m2.add(m2i1);
        m2.add(m2i2);
        m2.add(m2i3);
        m2.add(m2i4);
        m3.add(m3i1);
        m3.add(m3i2);
        m3.add(m3i3);
        m3.add(m3i4);
        m5.add(m5i1);
        m5.add(m5i2);
        m6.add(m6i1);
        m6.add(m6i2);
        m6.add(m6i3);
        m6.add(m6i4);
        m7.add(m7i1);
        m4.add(m4i1);
        
        menuPrincipal.add(m1);
        menuPrincipal.add(m2);
        menuPrincipal.add(m3);
        menuPrincipal.add(m5);
        menuPrincipal.add(m6);
        menuPrincipal.add(m7);
        menuPrincipal.add(m4);
    }

    private void m1i1ActionPerformed(ActionEvent e){ // Crear texto
        ci.nuevoTexto();
    }

    private void m1i4ActionPerformed(ActionEvent e){ // Salir
        System.exit(0);
    }

    private void m2i1ActionPerformed(ActionEvent e){ // Seleccionar todo
        ci.seleccionarTodo();
    }

    private void m2i2ActionPerformed(ActionEvent e){ // Copiar
        ci.copiarTexto();
    }

    private void m2i3ActionPerformed(ActionEvent e){ // Cortar
        ci.cortarTexto();
    }

    private void m2i4ActionPerformed(ActionEvent e){ // Pegar
        ci.pegarTexto();
    }

    private void m1i2ActionPerformed(ActionEvent e){ // Cargar texto
        ci.cargarTexto();
    }

    private void m1i3ActionPerformed(ActionEvent e){ // Salvar texto
        ci.salvarTexto();
    }

    private void m1i5ActionPerformed(ActionEvent e){ // Cerrar texto
        ci.cerrarTexto();
    }

    private void m4i1ActionPerformed(ActionEvent e){ // Mostrar ayuda
        cambiarVistas(false,false,false,false,true);
        ci.subirScrollAyuda();
    }

    private void cambiarVistas(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5) {
        //pre: minimo y maximo una debe estar en true
        ci.getVistaReglas().setVisible(b1);
        ci.getVistaDiccionario().setVisible(b2);
        ci.getVistaConjPares().setVisible(b3);
        ci.getVistaCorregir().setVisible(b4);
        ci.getVistaAyuda().setVisible(b5);
    }
    private void m3i1ActionPerformed(ActionEvent e){ // Crear diccionario
        cambiarVistas(false,true,false,false,false);
        ci.crearDiccionario();
    }

    private void m3i2ActionPerformed(ActionEvent e) throws ExcepcionVacio, ExcepcionExisteNombreDiccionario{ // Cargar diccionario
        cambiarVistas(false,true,false,false,false);
        ci.cargarDiccionario();
    }
   private void m3i3ActionPerformed(ActionEvent e){ // Salvar diccionario
        ci.salvarDiccionario();
    }
    private void m3i4ActionPerformed(ActionEvent e){ // Eliminar diccionario
        ci.eliminarDiccionario();
    }
    private void m5i1ActionPerformed(ActionEvent e){ // Empezar correción
        cambiarVistas(false,false,false,true,false);

    }
    private void m5i2ActionPerformed(ActionEvent e){ // Finalizar correción
        cambiarVistas(false,false,false,true,false);
    }
    private void m6i1ActionPerformed(ActionEvent e){ // Crear conjunto de pares
        cambiarVistas(false,false,true,false,false);
        ci.nuevoConjuntoPares();
  
    }
    private void m6i2ActionPerformed(ActionEvent e) throws FormatoConjParesNoValido, IOException{ // Cargar conjunto de pares
        cambiarVistas(false,false,true,false,false);
        ci.eliminarConjuntoPares2();
        ci.cargarConjuntoPares();
    }
    private void m6i3ActionPerformed(ActionEvent e){ // Salvar conjunto de pares
        cambiarVistas(false,false,true,false,false);
        ci.salvarConjuntoPares();

    }
    private void m6i4ActionPerformed(ActionEvent e){ // Eliminar conjunto de pares
        cambiarVistas(false,false,true,false,false);
        ci.eliminarConjuntoPares();
    }
    private void m7i1ActionPerformed(ActionEvent e) throws ExcepcionVacio{ //Generar Reglas
        cambiarVistas(true,false,false,false,false);
        ci.generarReglas(ci.getConjPares());
        ci.mostrarReglas();
    }
   
    private void inicializarToolbar(){
        toolbar.setFloatable(false);
        JButton btb1 = new JButton("",new ImageIcon(getClass().getResource("iconos/abrir.png")));
        btb1.setToolTipText("Cargar texto...");
        btb1.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
               ci.cargarTexto();
            }
        });
        JButton btb2 = new JButton("",new ImageIcon(getClass().getResource("iconos/nuevo.png")));
        btb2.setToolTipText("Crear texto");
        btb2.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
               nuevoTextoActionPerformed(e);
            }
        });
        JButton bclose = new JButton("");
        bclose.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
               cerrarTextoActionPerformed(e);
            }
        });

        JButton btb3 = new JButton("",new ImageIcon(getClass().getResource("iconos/salvar.png")));
        btb3.setToolTipText("Salvar texto...");
        btb3.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
               ci.salvarTexto();
            }
        });
        JButton btb4 = new JButton("",new ImageIcon(getClass().getResource("iconos/conjpares.png")));
        btb4.setToolTipText("Conjunto de pares");
        btb4.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
               btb4ActionPerformed(e);
            }
        });
        JButton btb5 = new JButton("",new ImageIcon(getClass().getResource("iconos/diccionario.png")));
        btb5.setToolTipText("Diccionario");
        btb5.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
               btb5ActionPerformed(e);
            }
        });
        JButton btb6 = new JButton("",new ImageIcon(getClass().getResource("iconos/reglas.png")));
        btb6.setToolTipText("Reglas");
        btb6.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
               btb6ActionPerformed(e);
            }
        });
        JButton btb7 = new JButton("",new ImageIcon(getClass().getResource("iconos/corregir.png")));
        btb7.setToolTipText("Corregir");
        btb7.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
               btb7ActionPerformed(e);
            }
        });
        toolbar.add(btb1);
        toolbar.add(btb2);
        toolbar.add(btb3);
        toolbar.addSeparator();
        toolbar.addSeparator();
        toolbar.add(btb4);
        toolbar.add(btb5);
        toolbar.add(btb6);
        toolbar.addSeparator();
        toolbar.addSeparator();
        toolbar.add(btb7);
    }
    private void inicializarPanelPrincipal(){

    }
    private void btb4ActionPerformed(ActionEvent e){
        cambiarAVistaConjPares();
    }
    private void btb5ActionPerformed(ActionEvent e){
        cambiarAVistaDiccionario();
    }
    private void btb6ActionPerformed(ActionEvent e){
        cambiarAVistaReglas();
    }
    private void btb7ActionPerformed(ActionEvent e){
        cambiarAVistaCorregir();
        ci.corregir();
    }
    private void inicializarLayout2(){

        panelPrincipal.setLayout(layout2);

        GridBagConstraints cons = new GridBagConstraints();
        panelPrincipal.setLayout(layout2);
        cons = new GridBagConstraints();
        cons.weightx = 0.6;

        cons.gridx=0;
        cons.gridy=0;
        cons.fill = GridBagConstraints.BOTH;
        cons.anchor = GridBagConstraints.WEST;
        cons.insets = new Insets(5,5,5,5);
        panelPrincipal.add(ci.getVistaTexto(),cons);


        cons.weightx = 0.4;
        cons.weighty=1;
        cons.gridx=1;
        cons.gridy=0;
        cons.insets = new Insets(5,5,5,5);
        cons.fill = GridBagConstraints.BOTH;
        cons.anchor = GridBagConstraints.WEST;
        
        panelPrincipal.add(ci.getVistaReglas(),cons);
        panelPrincipal.add(ci.getVistaDiccionario(), cons);
        panelPrincipal.add(ci.getVistaCorregir(),cons);
        panelPrincipal.add(ci.getVistaConjPares(),cons);
        panelPrincipal.add(ci.getVistaAyuda(),cons);
        ci.getVistaCorregir().setVisible(false);
        ci.getVistaReglas().setVisible(false);
        ci.getVistaConjPares().setVisible(false);
        ci.getVistaAyuda().setVisible(false);
    }
    private void montar(){

        this.getContentPane().add(menuPrincipal);
        this.getContentPane().add(toolbar);
        this.getContentPane().add(panelPrincipal);
        //JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelPrincipal, ci.cit.vi.getTabbedPane());
        //splitPane.setOneTouchExpandable(true);
        //splitPane.setDividerLocation(150);
    }
    private void nuevoTextoActionPerformed(ActionEvent evt){
        //ci.setVistaTexto(panelPrincipal.getComponent(0));
        ci.nuevoTexto();
    }
    private void cerrarTextoActionPerformed(ActionEvent evt){
        //ci.setVistaTexto(panelPrincipal.getComponent(0));
        ci.nuevoTexto();
    }
}
